package com.library.service.impl;

import com.library.entity.User;
import com.library.exception.LoginErrorException;
import com.library.exception.UserRepeatException;
import com.library.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by linzhi on 2016/12/12.
 */
@Service
@Transactional
public class UserService extends BaseService<User> implements IUserService {
    @Override
    public void register(User user) throws UserRepeatException {
        //1.判断用户名是否重复
        if (userDao.findByUsername(user.getUsername())!=null) {
            throw new UserRepeatException("用户名重复！");
        }
        //2.保存用户
        userDao.save(user);
    }

    @Override
    public User login(User user) throws LoginErrorException {
        //1.判读用户名是否存在
        User userReturn = userDao.findByUsername(user.getUsername());
        if (userReturn==null) {
            throw new LoginErrorException("用户不存在!");
        }
        //2.判读密码是否正确
        if (!userReturn.getPassword().equals(user.getPassword())) {
            throw new LoginErrorException("密码不正确!");
        }
        return userReturn;
    }

}
